package com.morningstar.qs.api.basic.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @deprecated
 * @author dchen2
 *
 */
public class StringUtil {

	private StringUtil() {
	};

	/**
	 * return a string which has amount space bars
	 * @param amount
	 * @return
	 */
	public static String getSpacebar(int amount) {
		StringBuffer sb = new StringBuffer();
		sb.append("");
		if (amount > 0) {
			for (int i = 0; i < amount; i++) {
				sb.append(" ");
			}
		}
		return sb.toString();
	}

	public static String formatDirPath(String dir, String end) {
		if (null == dir || 0 == dir.length() 
				|| null == end || 0 == end.length()) {
			return dir;
		}
		
		int len = dir.length();
		int idx = dir.lastIndexOf(end);
		
		String temp = dir.substring(idx + 1, len);
		if (null != temp && 0 < temp.length()) {
			dir = dir + end;
		}
		
		return dir;
	}

	public static String formatStringEnding(String dir, String end) {
		String dest = "";
		
		if (null == dir || 0 == dir.length() 
				|| null == end || 0 == end.length()) {
			return dest;
		}
	
		dir = dir.trim();
		end = end.trim();
		
		int len = dir.length();
		int idx = dir.lastIndexOf(end);
		
		String temp = dir.substring(idx + 1, len);
		if (null != temp && 0 < temp.length()) {
			dest += dir + end;
		} else {
			dest = dir;
		}
		
		return dest;
	}
	
	public static String filter4html(String str){ 
		if (null == str || 0 == str.length()) {
			return "";
		}
		
		str = str.replaceAll("&quot;", "\"\"");
		str = str.replaceAll("&lt;", "<");
        str = str.replaceAll("&gt;", ">");
        
        return str;
    }
	
	public static String formatDoubleToString(double d, String fmt) {
		DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
		format.applyPattern(fmt);
		String s = format.format(d);
		return s;
	}
}
